package com.maybach.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by well on 15/3/16.
 */
public class CollectionUtil {

    public static  final Transformer<String, Long> STRING_TO_LONG = new Transformer<String, Long>() {
        @Override
        public Long transform(String object) {
            return Long.parseLong(object);
        }
    };



    public static <S, T> List<T> transform(Collection<S> input, Transformer<S, T> transformer) {
        List<T> ret = new ArrayList<T>();
        if (input == null) {
            return ret;
        }
        for (S o : input) {
            ret.add(transformer.transform(o));
        }
        return ret;
    }

    public static <S, T> List<T> transform(S [] input, Transformer<S, T> transformer) {
        List<T> ret = new ArrayList<T>();
        if (input == null) {
            return ret;
        }
        for (S o : input) {
            ret.add(transformer.transform(o));
        }
        return ret;
    }

    public static <S> List<S> transform(S [] input) {
        List<S> ret = new ArrayList<S>();
        if (input == null) {
            return ret;
        }
        for (S o : input) {
            ret.add(o);
        }
        return ret;
    }

    public static String toString(List list , String seperator){
        if(null == list){
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for(Object obj : list){
            builder.append(obj).append(seperator);
        }
        if(builder.length() > 0){
            return builder.substring(0, builder.length() -1);
        }
        return "";
    }

    public static String toString(Object[] list , String seperator){
        if(null == list){
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for(Object obj : list){
            builder.append(obj).append(seperator);
        }
        if(builder.length() > 0){
            return builder.substring(0, builder.length() -1);
        }
        return "";
    }

}
